package com.sc.nft.admin.controller;


import com.sc.nft.admin.request.AddBoxPoolRequest;
import com.sc.nft.annotation.SysMemberLogin;
import com.sc.nft.dao.BoxTakePoolDao;
import com.sc.nft.dao.UserCreatorInfoDao;
import com.sc.nft.entity.BoxTakePool;
import com.sc.nft.entity.dto.QueryBoxConditionDTO;
import com.sc.nft.entity.vo.GetDaoByIdVO;
import com.sc.nft.entity.vo.QueryBoxConditionPackagingVO;
import com.sc.nft.entity.vo.QueryBoxConditionVO;
import com.sc.nft.service.BoxTakeConversionConditionService;
import com.sc.nft.sup.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * 盲盒兑换条件(BoxTakeConversionCondition)表控制层
 *
 * @author 王伟成
 * @since 2023-11-07 17:21:41
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin/boxTakeConversionCondition")
@Api(tags = "盲盒兑换条件")
public class BoxTakeConversionConditionAdminController {
    /**
     * 服务对象
     */
    private final BoxTakeConversionConditionService boxTakeConversionConditionService;

    private final BoxTakePoolDao boxTakePoolDao;

    private final UserCreatorInfoDao userCreatorInfoDao;


    @PostMapping(value = "/addBoxCondition", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "盲盒兑换条件新增", notes = "盲盒添加藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result addBoxCondition(@Valid @RequestBody AddBoxPoolRequest request) {
        boxTakeConversionConditionService.addBoxCondition(false, request.getBoxId(), null, request.getProductBoxId(), request.getUpperLimit(), request.getAddBoxConditionCollectionDTO(), request.getAddBoxCondition(), request.getConvertScenario(), request.getDaoId(),null);
        return Result.success();
    }

    @PostMapping(value = "/queryBoxCondition", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiOperation(value = "盲盒兑换条件查询", notes = "盲盒添加藏品", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @SysMemberLogin
    public Result<QueryBoxConditionPackagingVO> queryBoxCondition(@Valid @RequestBody QueryBoxConditionDTO queryBoxConditionDTO) {
        List<QueryBoxConditionVO> queryBoxConditionVOS = boxTakeConversionConditionService.queryBoxCondition(queryBoxConditionDTO);
        QueryBoxConditionPackagingVO queryBoxConditionPackagingVO = new QueryBoxConditionPackagingVO();
        queryBoxConditionPackagingVO.setQueryBoxCondition(queryBoxConditionVOS);
        // 转换结构
        queryBoxConditionPackagingVO.transition();
        //region 品牌id和品牌名称
        BoxTakePool boxTakePool = boxTakePoolDao.getById(queryBoxConditionDTO.getBoxId());
        if (Objects.nonNull(boxTakePool) && Objects.nonNull(boxTakePool.getDaoId())) {
            queryBoxConditionPackagingVO.setDaoId(boxTakePool.getDaoId());
            GetDaoByIdVO getDaoByIdVO = userCreatorInfoDao.getDaoById(boxTakePool.getDaoId());
            if (Objects.nonNull(getDaoByIdVO)) {
                queryBoxConditionPackagingVO.setDaoName(getDaoByIdVO.getDaoName());
            }
        }
        //endregion
        return Result.success(queryBoxConditionPackagingVO);
    }


}
